package res;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

public class ImageList {
	
	public static ArrayList<String> laundryok(String query) {
		ArrayList<String> list = new ArrayList<String>();
		String html = getHTML("http://openapi.naver.com/search?key=8fdb5e3f8259ce036f458d96b90b6320&query="+query+"&target=image&start=1&display=10");
		list=parsingHTML(html, new String[] { "link", "title" });
		return list;
	}

	static String getHTML(String data) {
		StringBuilder sb = new StringBuilder();
		try {
			String line = "";
			InputStream is = new URL(data).openStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is,
					"UTF-8"));
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	static ArrayList<String> parsingHTML(String html, String[] tags) {
		ArrayList<String> datas = new ArrayList<String>();
		int startIndex = 0;
		boolean isThatTag = false;
		for (int i = 0; i < html.length(); i++) {
			if (html.charAt(i) == '<') {
				for (int j = 0; j < tags.length; j++) {
					if (html.substring(i + 1, i + 1 + tags[j].length())
							.equalsIgnoreCase(tags[j])) {
						startIndex = i;
						isThatTag = true;
					} else if (isThatTag
							&& html.substring(i + 2, i + 2 + tags[j].length())
									.equalsIgnoreCase(tags[j])) {
						datas.add(html.substring(
								startIndex + 2 + tags[j].length(), i));
						isThatTag = false;
					}
				}
			}
		}
		return datas;
}
}
